// components/Blank/blank.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    pageData: {
      type: Object
    },
    CustID: {
      type: String
    },
    UnionStoreID: {
      type: String
    }
   

  },
  ready: function() {
    var that = this;

    var globalData = getApp().globalData;
    var postData = {
      custID: this.properties.CustID,
      ObjectID: this.properties.UnionStoreID
    };
    wx.request({
      url: globalData.apiurl + '/magic/api/MagicMall/NowSecondKillActivity',
      data: JSON.stringify(postData),
      method: 'post',
      header: {
        'content-type': 'application/json',
        'X_ManuName': globalData.manuName
      },
      success: function(res) {
        var m = res.data.content;
        if (m == null) {
          that.setData({
            hideSkill: true

          });
          return;
        }

        var NowTime = new Date();
        var timestart = ChansferDate(m.BeginDt);
        var timeout = ChansferDate(m.EndDt);
        if (m.Schedule != null && m.Schedule != "") {
          timeout = ChansferDate(m.Tomorrow);
        }
        var endDiscribr = "";
        if ((NowTime - timestart) > 0 && (timeout - NowTime) > 0) {

          endDiscribr = "距活动结束";
        } else if ((NowTime - timeout) >= 0) {

          endDiscribr = "距活动结束";
        } else if ((timestart - NowTime) > 0) {
          endDiscribr = "距活动开始";
        }
        that.setData({
          EndDiscribr: endDiscribr,

        });



        setInterval(function() {

          var nowTime = new Date();

          var t = 0;
          //已经开始
          if ((timeout - nowTime) > 0 && (nowTime - timestart) > 0) {

            t = timeout - nowTime;
          }
          //还没开始
          if ((timestart - nowTime) > 0) {
            t = timestart - nowTime;

          }


          if (t > 0) {
            var d = Math.floor(t / 1000 / 60 / 60 / 24);
            var h = Math.floor(t % (24 * 3600 * 1000) / 1000 / 60 / 60);
            var m = Math.floor(t / 1000 / 60 % 60);
            var s = Math.floor(t / 1000 % 60);

            if (h.toString().length <= 1) {
              h = "0" + h;
            }
            if (m.toString().length <= 1) {
              m = "0" + m;
            }
            if (s.toString().length <= 1) {
              s = "0" + s;
            }


            that.setData({
              date: d,
              Hour: h,
              Minute: m,
              second: s
            });
          }
        }, 1000);



      }
    })
   
    that.setData({ imgDomain: globalData.imgurl});
  
    wx.request({
      url: globalData.apiurl + '/magic/api/Activity/GetSecondKillProducts',
      data: {
        "custID": this.properties.CustID,
        "unionStoreID": this.properties.UnionStoreID
      },
      method: 'get',
      header: {
        'content-type': 'application/json',
        'X_ManuName': globalData.manuName
      },
      success: function(responseData) {
        var element = responseData.data.content;
        that.setData({ SecondKillProducts: element})

      }

    });

    

 

  },


  /**
   * 组件的初始数据
   */
  data: {
    date: "0",
    Hour: "00",
    Minute: "00",
    second: "00",
    EndDiscribr: "",
    hideSkill: false,
    SecondKillProducts:null,
    imgDomain:""

  },

  /**
   * 组件的方法列表
   */
  methods: {

  }
})

function ChansferDate(strdate) {
  var arr = strdate.split(/[- : \/]/);
  var date = new Date(arr[0], arr[1] - 1, arr[2], arr[3], arr[4], arr[5]);
  return date;
}